package com.jdew.thkj.utils;

import android.support.annotation.NonNull;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * @author:peixianzhong
 * @emil:530927342@qq.com
 * @date:2018/8/21_11:06
 * @description:日期时间处理工具类
 */
public class DateTimeUtils {
    private static final ThreadLocal<SimpleDateFormat> SDF_THREAD_LOCAL = new ThreadLocal<>();
    private static SimpleDateFormat matter1 = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat getDefaultFormat() {
        SimpleDateFormat simpleDateFormat = SDF_THREAD_LOCAL.get();
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
            SDF_THREAD_LOCAL.set(simpleDateFormat);
        }
        return simpleDateFormat;
    }

   /* private static SimpleDateFormat getDefaultFormat() {
        SimpleDateFormat simpleDateFormat = SDF_THREAD_LOCAL.get();
        if (simpleDateFormat == null) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            SDF_THREAD_LOCAL.set(simpleDateFormat);
        }
        return simpleDateFormat;
    }*/

    /**
     * 获取当前年
     */
    public static int getNowYear() {
        Calendar c = Calendar.getInstance();
        int mYear = c.get(Calendar.YEAR);
        return mYear;
    }

    /**
     * 获取当前月
     */
    public static int getNowMonth() {
        Calendar c = Calendar.getInstance();
        int mMonth = c.get(Calendar.MONTH) + 1;
        return mMonth;
    }

    /**
     * 获取当前日
     */
    public static int getNowDay() {
        Calendar c = Calendar.getInstance();
        int mDay = c.get(Calendar.DAY_OF_MONTH);
        return mDay;
    }

    /**
     * 获取当前时间并且格式化（yyyy-MM-dd）（yyyyMMdd）（yyyy年MM月dd日）（yyyy-MM-dd HH:mm:ss）（yyyyMMddHHmmss）（yyyy年MM月dd日HH时mm分ss秒）
     */
    public static String getNowDate(int type) {
        if (type == 1) {
            return new SimpleDateFormat("yyyy年MM月dd日").format(getNowDate());
        } else if (type == 2) {
            return new SimpleDateFormat("yyyyMMdd").format(getNowDate());
        } else if (type == 3) {
            return new SimpleDateFormat("yyyy-MM-dd").format(getNowDate());
        } else if (type == 4) {
            return new SimpleDateFormat("yyyy年MM月dd日HH时mm分ss秒").format(getNowDate());
        } else if (type == 5) {
            return new SimpleDateFormat("yyyyMMddHHmmss").format(getNowDate());
        } else if (type == 6) {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getNowDate());
        }
        return new SimpleDateFormat("yyyy-MM-dd").format(getNowDate());
    }

    /**
     * 获取当前date
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前时间字符串
     */
    public static String getNowString() {
        return millis2String(System.currentTimeMillis(), getDefaultFormat());
    }

    /**
     * 获取当前时间戳
     */
    public static long getNowMills() {
        return System.currentTimeMillis();
    }

    /**
     * 判断今年是否是闰年
     */
    public static boolean isLeapYear() {
        return isLeapYear(getNowYear());
    }

    /**
     * 判断是否是闰年
     */
    public static boolean isLeapYear(final int year) {
        return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
    }

    /**
     * 将时间戳转为时间字符串
     */
    public static String millis2String(final long millis) {
        return millis2String(millis, getDefaultFormat());
    }

    /**
     * 将时间戳转为时间字符串
     */
    public static String millis2String(final long millis, @NonNull final DateFormat format) {
        return format.format(new Date(millis));
    }

    /**
     * 将时间字符串转为时间戳
     */
    public static long string2Millis(final String time) {
        return string2Millis(time, getDefaultFormat());
    }

    /**
     * 将时间字符串转为时间戳
     */
    public static long string2Millis(final String time, @NonNull final DateFormat format) {
        try {
            return format.parse(time).getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return -1;
    }

    /**
     * 将时间字符串转为 Date 类型
     */
    public static Date string2Date(final String time) {
        return string2Date(time, getDefaultFormat());
    }

    /**
     * 将时间字符串转为 Date 类型
     */
    public static Date string2Date(final String time, @NonNull final DateFormat format) {
        try {
            return format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 将 Date 类型转为时间字符串
     */
    public static String date2String(final Date date) {
        return date2String(date, getDefaultFormat());
    }

    /**
     * 将 Date 类型转为时间字符串
     */
    public static String date2String(final Date date, @NonNull final DateFormat format) {
        return format.format(date);
    }

    /**
     * 将 Date 类型转为时间戳
     */
    public static long date2Millis(final Date date) {
        return date.getTime();
    }

    /**
     * 将时间戳转为 Date 类型
     */
    public static Date millis2Date(final long millis) {
        return new Date(millis);
    }


    /**
     * date的上2个月
     */
    public static Date getLast2Month(Date date) {
        //过去一月
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -2);
        Date m = c.getTime();
        return m;
    }

    /**
     * date的上一天
     */
    public static Date getYestoday(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        Date m = calendar.getTime();
        return m;
    }
    /**
     * date的前7天
     */
    public static Date get7dayago(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, -7);
        Date m = calendar.getTime();
        return m;
    }

    public  static String getWeekMonday(){
        Calendar cal=new GregorianCalendar();
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        cal.setTime(new Date());
        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        Date first=cal.getTime();
        return matter1.format(first);
    }

    public static String getTimeOfMonthStart() {                  //获取本月第一天
        Calendar ca = Calendar.getInstance();
        ca.set(Calendar.HOUR_OF_DAY, 0);
        ca.set(Calendar.DAY_OF_MONTH, 1);
        Date d = ca.getTime();
        return matter1.format(d);
    }

    public static String getTimeOfLastMonthStart() {             //获取上月第一天
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        Date d = calendar.getTime();
        return matter1.format(d);
    }

    public static String getTimeOfLastMonthEnd() {               //获取上月最后一天
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.DATE, -1);
        Date d = calendar.getTime();
        return matter1.format(d);
    }
}